
import json
import os
import sys

# print(sys.path)
# 由于本脚本在 tests 目录，任何情况下将只以此脚本所在目录为根目录
# 如果是使用 python3 运行此脚本，确保在编译器的时候加入当前目录(确保在项目目录下)
sys.path.append(os.path.abspath(".")) 
# 如果是使用 Code Runner 插件运行此脚本，确保在 cd <paths>/test 的时候加入父级目录
sys.path.append(os.path.abspath("..")) 
# print(sys.path)

from project.base import *

# MainWindow 一个来自 mainwindow.ui 的
from ui_mainwindow import Ui_MainWindow

from PyQt5.QtWidgets import QSizePolicy


# Generated by https://quicktype.io


class DataStruct(QObject):
    label: QLabel
    complete: QCheckBox
    line : QLineEdit
    btn : QPushButton
    index: int
    removed: bool
    
    textChanged = pyqtSignal(str)
    checkChanged = pyqtSignal(int)
    deleted = pyqtSignal()    

    def __init__(self, index) -> None:
        super(DataStruct, self).__init__()
        # self.complete = complete
        # self.label = label
        self.label = QLabel(str(index))
        self.complete = QCheckBox()
        self.line = QLineEdit(str(index))
        self.btn = QPushButton("Del")
        self.index = index
        self.removed = False
        
        
        self.complete.stateChanged.connect(self.checkChanged)
        self.complete.stateChanged.connect(lambda x: print(x))
        
        self.line.textChanged[str].connect(self.textChanged)
        self.btn.clicked.connect(self.remove)
        self.btn.clicked.connect(lambda: print("del"))
        self.btn.clicked.connect(self.deleted)
        
    @pyqtSlot()
    def remove(self):
        self.label.setParent(None)
        self.complete.setParent(None)
        self.line.setParent(None)
        self.btn.setParent(None)
        # self.index.setParent(None)
        self.removed = True
        
    def setLayout(self, layout: QHBoxLayout):
        layout.addWidget(self.label)
        layout.addWidget(self.complete)
        layout.addWidget(self.line)
        layout.addWidget(self.btn)
        
    def isDeleted(self):
        return self.removed
        

class MainWindow(QMainWindow):
    
    dss : list
    
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setCentralWidget(QWidget())
        self.dss = []
        
        self.hbox = QHBoxLayout(self.centralWidget())
        
        self.left = QTextEdit()
        self.right= QWidget()
        self.right1 = QWidget()
        self.right2 = QWidget()
        
        self.hbox.addWidget(self.left)
        self.hbox.addWidget(self.right)
        
        self.vbox  = QVBoxLayout(self.right)
        self.vbox1 = QVBoxLayout()
        self.vbox2 = QVBoxLayout()
        
        self.vbox.addLayout(self.vbox1)
        self.vbox.addLayout(self.vbox2)
        
        self.addVBoxItems(0)
        self.addVBoxItems(1)
        self.addVBoxItems(2)
        # self.addVBoxItems(3)

        verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
        self.add = QPushButton("+")
        self.add.clicked.connect(lambda: self.addVBoxItems(10))
        
        self.vbox2.addItem(verticalSpacer)
        self.vbox2.addWidget(self.add)
 
        
    def addVBoxItems(self, index):
        hbox = QHBoxLayout()
        ds = DataStruct(index)
        ds.setLayout(hbox)
        ds.checkChanged.connect(self.regenerate)
        ds.textChanged.connect(self.regenerate)
        ds.deleted.connect(self.regenerate)
        self.vbox1.addLayout(hbox)
        self.dss.append(ds)
        
        self.regenerate()
        
    def delVBoxItem(self, index):
        print("删除：",index)
        self.vbox1.removeItem(self.vbox1.takeAt(index))
        
        
    def regenerate(self):
        objs = []
        ds :DataStruct
        for ds in self.dss:
            if ds.isDeleted():
                continue
            objs.append(
                {
                    "completed": ds.complete.isChecked(),
                    "todo": ds.line.text()
                }
            )
            
        self.left.setPlainText(json.dumps(objs, indent=2))
            
        

# 一个动画居中窗口
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    animationMoveWidgetCenterInDesktopWidget(window)
    sys.exit(app.exec_())
